

#coding=utf-8
import time  
from selenium import webdriver  
import re
import tkinter as tk

driver = webdriver.Chrome(r'C:\Users\guosu\Desktop\Justins\python\chromedriver.exe')  #打开浏览器
upperlimit_gold = 405
lowerlimit_gold = 399
upperlimit_sliver = 4550
lowerlimit_sliver = 4400  

def batch_check_interest_rate(driver):

    # for home == r'F:\Python\Economic\chromedriver_win32\chromedriver.exe'
        
    elem = driver.get('https://finance.sina.com.cn/futures/quotes/AUTD.shtml')  #打开网址
    information = "" #把结果要保存的变量先初始化好
    open_price_final = "" #把开盘价先初始化好
    twenty_average = ""

    def get_info(day_date=0):   #定义主要方法： 复制利率到information变量，并同时打印出来 
        nonlocal information,open_price_final,twenty_average #调用父方法的变量 
        status = driver.page_source #把全网页源代码文本保存在status变量里 
        price = status[status.find('real-price price'):status.find('change-wrap')] #找到第二个/tr后的内容，就是说提取前两行信息的意思
        open_price = status[status.find('open-price'):status.find('max-price')]
        filtered_open_price = re.findall('">(.*?)</',open_price)
        filtered_price = re.findall('">(.*?)</',price)
        if day_date==1:
            driver.find_element_by_xpath('.//*[@data-id="KKE_tab_kd"]').click()
            time.sleep(3)
            status = driver.page_source #把刷新后的网页源代码文本保存在status变量里 
            twenty_average_try = status[status.find('MA20:'):status.find('MA30: ')]
            filtered_20_average = re.findall('>(.*?)</',twenty_average_try)
            twenty_average = twenty_average_try[6:12]
        #print('The latest rate is: \n')
        information = filtered_price[0]
        open_price_final = filtered_open_price[0]
        #print('\n')  #前后空好行 

    # 正式开始执行查询近期rate任务
    #print('for US:\n')

    get_info()
    print(information)  #根据需要打印出利率详情统计。
    print("Open price is "+open_price_final)

    def popup(a=1):
        if a==1:
            root = tk.Tk()
            root.wm_attributes('-topmost',1)
            root.title("价格提醒")
            root.geometry('500x300')
            label = tk.Label(root, text='Gold price is:\n'+information)
            label.pack()
            root.mainloop()
        if a==2:
            root = tk.Tk()
            root.wm_attributes('-topmost',1)
            root.title("可加仓")
            root.geometry('500x300')
            label = tk.Label(root, text='Gold price is:\n'+open_price_final-information)
            label.pack()
            root.mainloop()
        if a==3:
            root = tk.Tk()
            root.wm_attributes('-topmost',1)
            root.title("减仓")
            root.geometry('500x300')
            label = tk.Label(root, text='Gold price is:\n'+information-open_price_final)
            label.pack()
            root.mainloop()
        if a==4:
            root = tk.Tk()
            root.wm_attributes('-topmost',1)
            root.title("小心，出现了极端情况！！！")
            root.geometry('500x300')
            label = tk.Label(root, text='Gold price is:\n'+information-open_price_final)
            label.pack()
            root.mainloop()
                    
    if float(information) > upperlimit_gold or float(information) < lowerlimit_gold:
        popup()


    #check out AG trending    
    elem = driver.get('https://finance.sina.com.cn/futures/quotes/AGTD.shtml')
    information = "" #把结果要保存的变量先初始化好
    get_info()
    print(information)  #根据需要打印出利率详情统计。
    print("AG Open price is "+open_price_final)
    
    if float(information) > upperlimit_sliver or float(information) < lowerlimit_sliver:
        popup()

    
    #check out CME gold trending    
    elem = driver.get('https://finance.sina.com.cn/futures/quotes/GC.shtml')
    information = "" #把结果要保存的变量先初始化好
    get_info(1)
    print(information)  #根据需要打印出利率详情统计。
    print("CME Open price is "+open_price_final)
    print("20 day average is "+twenty_average+". \n And the difference between 20 day and now is (Normally is should be within 48 and 95% in 76) "+str(abs(float(twenty_average)-float(open_price_final))))

    # comparing open with close
    if (float(information)-float(open_price_final)) > 19:
        popup(3)
    if abs(float(information)-float(open_price_final)) > 50:
        popup(4)
    if (float(open_price_final)-float(information)) > 19:
        popup(2)

    # compare with average
    if (float(information)-float(twenty_average)) > 48:
        popup(4)
    if abs(float(information)-float(open_price_final)) > 76:
        popup(4)

while True:
    batch_check_interest_rate(driver)
    time.sleep(300)
